﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hack.Generators.SerialGenerators
{
    public enum SNKeyLength
    {
        SN16 = 16, SN20 = 20, SN24 = 24, SN28 = 28, SN32 = 32
    }
    public enum SNKeyNumLength
    {
        SN4 = 4, SN8 = 8, SN12 = 12
    }
    public enum SNDelimiter
    {
        None = 0, Tab = 1, Space = 2, Hyphen = 3, Period = 4, Slash = 5, Comma = 6
    }
    /// <summary>
    /// SNK = xxxx-xxxx-xxxx-xxxx-xxxx-xxxx
    /// </summary>
    public static class SNKSerialGenerator
    {
        private static string convertDelimiter(SNDelimiter delInt)
        {
            String temp = "";
            switch (Convert.ToInt32(delInt))
            {
                case 0:
                    temp = "";
                    break;
                case 1:
                    temp = "\t";
                    break;
                case 2:
                    temp = " ";
                    break;
                case 3:
                    temp = "-";
                    break;
                case 4:
                    temp = ".";
                    break;
                case 5:
                    temp = "/";
                    break;
                case 6:
                    temp = ",";
                    break;
            }
            return temp;
        }
        private static string AppendSpecifiedStr(int length, int delLength, string str, char[] newKey)
        {

            string newKeyStr = "";
            int k = 0;
            for (int i = 0; i < length; i++)
            {
                if (delLength != 0)
                {
                    for
                    (k = i; k < delLength + i; k++)
                    {
                        newKeyStr += newKey[k];
                    }
                }
                if (k == length)
                {
                    break;
                }
                else
                {

                    if (delLength != 0)
                    {
                        i = (k) - 1;
                        newKeyStr += str;
                    }
                }
            }
            return newKeyStr;
        }

        /// <summary>
        /// Generate standard serial key with alphanumaric format the supported length of the serial key
        /// </summary>
        /// <param name="keyLength"></param>
        /// <param name="delimiter"></param>
        /// <param name="delimiterLength"></param>
        /// <returns>Formated serial key</returns>
        public static string GetSerialKeyAlphaNumeric(SNKeyLength keyLength, SNDelimiter delimiter, int delimiterLength)
        {
            String del;
            del = convertDelimiter(delimiter);
            Guid newguid = Guid.NewGuid();
            string randomStr = newguid.ToString("N");
            string tracStr = randomStr.Substring(0, (int)keyLength);
            tracStr = tracStr.ToUpper();
            char[] newKey = tracStr.ToCharArray();
            string newSerialNumber = "";
            switch (keyLength)
            {
                case SNKeyLength.SN16:
                    newSerialNumber = AppendSpecifiedStr(16, delimiterLength, del, newKey);
                    break;
                case SNKeyLength.SN20:
                    newSerialNumber = AppendSpecifiedStr(20, delimiterLength, del, newKey);
                    break;
                case SNKeyLength.SN24:
                    newSerialNumber = AppendSpecifiedStr(24, delimiterLength, del, newKey);
                    break;
                case SNKeyLength.SN28:
                    newSerialNumber = AppendSpecifiedStr(28, delimiterLength, del, newKey);
                    break;
                case SNKeyLength.SN32:
                    newSerialNumber = AppendSpecifiedStr(32, delimiterLength, del, newKey);
                    break;
            }
            return newSerialNumber;
        }
        /// <summary>
        /// Generate serial key with only numaric the supported length of the serial key
        /// </summary>
        /// <param name="keyLength"></param>
        /// <param name="delimiter"></param>
        /// <param name="delimiterLength"></param>
        /// <returns>Formated serial key</returns>
        public static string GetSerialKeyNumeric(SNKeyNumLength keyLength)
        {
            Random rn = new Random();
            double sd = System.Math.Round(rn.NextDouble() * System.Math.Pow(10, (int)keyLength) + 4);
            return sd.ToString().Substring(0, (int)keyLength);
        }
    }
}
